<?php
if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');
global $_GPC,$_W;
$pro_id= intval($_GPC['pro_id']);
$uniacid = $_W['uniacid'];
if(empty($pro_id)){
    message('抱歉，传递的参数错误！','', 'error');              
}

$lists = pdo_fetchall("SELECT * FROM " . tablename("tim_welfare_fund") . " WHERE pro_id =:pro_id AND fund_true=:fund_true  ORDER BY fund_time DESC ", array(":pro_id"=>$pro_id, ":fund_true"=>true));
$set =  pdo_fetch("SELECT * FROM ".tablename('tim_welfare_set')." WHERE uniacid=:uniacid",array(':uniacid'=>$uniacid));
$list = array();

foreach ($lists as $k) {
    $mem_name = pdo_fetch("SELECT *  FROM ".tablename('tim_welfare_member')."WHERE mem_id = :mem_id", array(':mem_id' => $k['mem_id']));
    $pro_name = pdo_fetch("SELECT pro_name  FROM ".tablename('tim_welfare_project')."WHERE pro_id = :pro_id", array(':pro_id' => $k['pro_id']));
    $rows = array( 
        'fund_id'    => $k['fund_id'],
        'pro_name'  => $pro_name['pro_name'],
        'mem_photo'  => $mem_name['mem_photo'],
        'mem_name'   => $mem_name['mem_name'],
        'order_num'  => $k['order_num'],
        'fund_value' => $k['fund_value'],
        // 'fund_time' => $k['fund_time']
        'fund_time' => ($k['fund_time'] == 0 ? '' : date('Y-m-d H:i',$k['fund_time']))
    );
    array_push($list, $rows);
}




$columns_head = array(
    0 => array('title' => "报表数据", 'height'    => 40, 'width'     => 180, 'colspan'   => 5, 'align'     => 'center', ), 
    1 => array('title'     => "日期：".date('Y-m-d',time()), 'height'    => 40, 'width'     => 180, 'colspan'   => 5, 'align'     => 'center', )
);

$columns = array(
    array('title' => '昵称' , 'field' => 'mem_name' ,'width' => 150),
    array('title' => '订单号' , 'field' => 'order_num' ,'width' => 200),
    array('title' => '项目名称' , 'field' => 'pro_name' ,'width' => 200),
    array('title' => '支持金额' , 'field' => 'fund_value' ,'width' => 200),
    array('title' => '支持时间' , 'field' => 'fund_time' ,'width' => 200),
);
$result = outputXlsHeader($columns,$list,date('Y-m-d',time())."数据统计",$columns_head);

function outputXlsHeader($columns,$exportlist,$filename="数据统计",$columns_head,$columns_foot){
    header("Content-type:text/html;charset=UTF-8;" );
    header('Content-Type: text/xls;');
    header("Content-type:application/vnd.ms-excel;charset=UTF-8;" );
    header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    $table_data = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">';
    $table_data .= '<head>';
    $table_data .= '<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">';
    $table_data .= '<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name></x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->';
    $table_data .= '</head>';
    $table_data .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
    $table_data .= '<table border="1">';
    
    if(!empty($columns_head)){
        foreach ($columns_head as $item){
            $table_data .="<tr>";
            if(empty($item['child'])){
                $table_data .="<td height='".$item['height']."' width='".$item['width']."' rowspan='".$item['rowspan']."' colspan='".$item['colspan']."' align='".$item['align']."'>".$item['title']."</td>";
            }else{
                foreach ($item['child'] as $child){
                    $table_data .="<td height='".$child['height']."' width='".$child['width']."' rowspan='".$child['rowspan']."' colspan='".$child['colspan']."' align='".$child['align']."'>".$child['title']."</td>";
                }
            }
            $table_data .="</tr>";
        }
    }
    
    $table_data .= '<tr>';
    foreach ($columns as $column){
        $height = 40;
        $width = empty($column['width']) ? 100 : $column['width'];
        $table_data .="<td height='".$height."' width='".$width."'>".$column['title']."</td>";
    }
    $table_data .= '</tr>';
    $table_data .= '<tr>';
    foreach ($exportlist as $line){
        $len = count($columns);
        for ($i = 0; $i < $len; $i++) {
            $height = empty($columns[$i]['height']) ? 30 : $columns[$i]['height'];
            $width = empty($columns[$i]['width']) ? 100 : $columns[$i]['width'];
            
            $value = isset($line[$columns[$i]['field']]) ? $line[$columns[$i]['field']] : '';
            $table_data .="<td height='".$height."' width='".$width."'>" . $value . "</td>";
        }
        $table_data .= '</tr>';
    }
    
    if(!empty($columns_foot)){
        foreach ($columns_foot as $item){
            $table_data .="<tr>";
            if(empty($item['child'])){
                $item['height'] = empty($item['height']) ? 40 : $item['height'] ;
                $item['width']  = empty($item['width']) ? 180 : $item['width'] ;
                $table_data .="<td height='".$item['height']."' width='".$item['width']."' rowspan='".$item['rowspan']."' colspan='".$item['colspan']."' align='".$item['align']."'>".$item['title']."</td>";
            }else{
                foreach ($item['child'] as $child){
                    $child['height'] = empty($child['height']) ? 30 : $child['height'] ;
                    $child['width']  = empty($child['width']) ? 180 : $child['width'] ;
                    $table_data .="<td height='".$child['height']."' width='".$child['width']."' rowspan='".$child['rowspan']."' colspan='".$child['colspan']."' align='".$child['align']."'>".$child['title']."</td>";
                }
            }
            $table_data .="</tr>";
        }
    }
    
    $table_data .='</table>';
    echo $table_data;
    die();
}
